package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.bean.FileBean;
import flc.ast.databinding.ActivityRecentFileBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.dialog.FormatDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class RecentFileActivity extends BaseAc<ActivityRecentFileBinding> {

    private FileAdapter fileAdapter;
    private List<String> listSel = new ArrayList<>();
    private boolean isEdit = false;
    private int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_recent_file;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecentFileBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivEdit.setOnClickListener(this);
        mDataBinding.tvAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
        mDataBinding.ivAll.setOnClickListener(this);
        mDataBinding.ivVideo.setOnClickListener(this);
        mDataBinding.ivPicture.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new GridLayoutManager(mContext, 3));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAll://全部
                clearSelection();
                type = 0;
                mDataBinding.ivAll.setImageResource(R.drawable.dj_qb);
                getAllData();
                break;
            case R.id.ivVideo://视频
                clearSelection();
                type = 1;
                mDataBinding.ivVideo.setImageResource(R.drawable.dj_spa);
                getVideoData();
                break;
            case R.id.ivPicture://图片
                clearSelection();
                type = 2;
                mDataBinding.ivPicture.setImageResource(R.drawable.dj_tpa);
                getPictureData();
                break;
            case R.id.ivEdit://编辑
                isEdit = true;
                fileAdapter.setEdit(isEdit);
                fileAdapter.notifyDataSetChanged();
                mDataBinding.llEdit.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAllSel://全选
                allSel();
                break;
            case R.id.tvDelete://删除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_file));
                    return;
                }
                showDeleteDialog();
                break;
            case R.id.ivComplete://完成
                cancelEdit();
                break;

        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit){
            if (fileAdapter.getItem(position).isSelected()) {
                fileAdapter.getItem(position).setSelected(false);
                listSel.remove(fileAdapter.getItem(position).getPath());
            } else {
                fileAdapter.getItem(position).setSelected(true);
                listSel.add(fileAdapter.getItem(position).getPath());
            }
            fileAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        getAllData();
    }

    private void getAllData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                list.add(new FileBean(file.getPath()));
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getVideoData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                if (!ImageUtils.isImage(file)) {
                    list.add(new FileBean(file.getPath()));
                }
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getPictureData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                if (ImageUtils.isImage(file)) {
                    list.add(new FileBean(file.getPath()));
                }
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void allSel() {
        listSel.clear();
        for (FileBean validDatum : fileAdapter.getValidData()) {
            listSel.add(validDatum.getPath());
            validDatum.setSelected(true);
        }
        fileAdapter.notifyDataSetChanged();
    }

    private void showDeleteDialog() {
        DeleteDialog deleteDialog = new DeleteDialog(mContext);
        deleteDialog.setListener(new DeleteDialog.Ilistener() {
            @Override
            public void clickDelete() {
                delete();
            }
        });
        deleteDialog.show();
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        for (String s : listSel) {
                            FileUtils.delete(s);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        cancelEdit();
                    }
                });
            }
        }, 1000);
    }

    private void cancelEdit() {
        isEdit = false;
        fileAdapter.setEdit(false);
        if (type == 0) {
            getAllData();
        } else if (type == 1) {
            getVideoData();
        } else if (type == 2) {
            getPictureData();
        }
        mDataBinding.llEdit.setVisibility(View.GONE);
    }

    private void clearSelection() {
        listSel.clear();
        mDataBinding.ivAll.setImageResource(R.drawable.dj_qba);
        mDataBinding.ivVideo.setImageResource(R.drawable.dj_sp);
        mDataBinding.ivPicture.setImageResource(R.drawable.dj_tp);
    }

}